package c.w.base.proxy;
/**************车载用户基本数据操作（增、改、查）*****************/
import java.util.ArrayList;

import c.w.base.dto.Car;
import c.w.base.dto.CarProductNumber;
import c.w.db.ConnDB;



public class CarBaseOperation {
	/**
	 * 公共变量
	 */
	ConnDB db = new ConnDB();
	
	/**
	 * 添加一个车载用户
	 * @author PHY
	 * @param c_city  车载用户所在城市
	 * @param c_id  车载用户id
	 * @param taxi_company  出租车公司
	 * @param car_number  车牌号
	 * @param name  姓名
	 * @param cellphone_number  手机号
	 * @param permit_drive_credentials_number  准驾证号
	 * @param friendly_score  友好积分
	 * @param integrity_score  诚信积分
	 * @param integrity_id  外键——与诚信表关联，是诚信表id：主键
	 * @return  true:添加成功	false:添加失败
	 */
	public boolean addCar(String c_city, int c_id, String c_password, String taxi_company, String car_number, String name, String cellphone_number,
						  String permit_drive_credentials_number,long friendly_score, long integrity_score, int integrity_id){
		
		boolean isAddCar = db.addCar(c_city, c_id, c_password, taxi_company, car_number, name, cellphone_number, permit_drive_credentials_number, friendly_score, integrity_score, integrity_id);
		boolean isAddIntegrity = db.addCarIntegrity(c_city, integrity_id);
		if (isAddCar && isAddIntegrity){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 添加车载用户的数据统计
	 * @author PHY
	 * @param c_city 车载用户所在城市
	 * @param c_id 车载用户id
	 * @param active_time 激活时间
	 * @param login_time  登录时间
	 * @param login_city  登录城市
	 * @param login_IP  登录IP
	 * @return
	 */
	public boolean addCarDatacount(String c_city, int c_id, String active_time, String login_time, String login_city, String login_IP){
		boolean idAddCarDatacount = db.addCarDataCount(c_city, c_id, active_time, login_time, login_city, login_IP);
		if (idAddCarDatacount){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 更新车载用户数据统计
	 * @param c_city 车载用户所在城市
	 * @param c_id 车载用户id
	 * @param login_time 登录时间
	 * @param login_city 登录城市
	 * @param login_IP 登录IP
	 * @return
	 */
	public boolean updateCarDatacount(String c_city, int c_id, String login_time, String login_city, String login_IP){
		boolean isUpdateCarDatacount = db.updateCarDatacount(c_city, c_id, login_time, login_city, login_IP);
		if (isUpdateCarDatacount){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 更新一个车载用户友好积分和诚信积分
	 * @author PHY
	 * @param c_city  车载用户所在城市
	 * @param c_id  车载用户id
	 * @param friendly_score  友好积分
	 * @param integrity_score  诚信积分
	 * @return true:更新成功		false:更新失败
	 */
	public boolean updateCarFriendlyAndIntegrityScore(String c_city, int c_id, long friendly_score, long integrity_score){
		boolean isUpdate = db.updateCarFriendlyAndIntegrityScore(c_city, c_id, friendly_score, integrity_score);
		if (isUpdate){
			return true; 
		}else{
			return false;
		}
	}
	
	/**
	 * 更新一个车载用户诚信度
	 * @author PHY
	 * @param c_city  车在用户所在城市
	 * @param integrity_id  诚信度id
	 * @param integrity  诚信度
	 * @param exposed_times  被举报次数
	 * @return true:更新成功		false:更新失败
	 */
	public boolean updateCarIntegrity(String c_city, int integrity_id, long integrity, int exposed_times){
		boolean isUpdate = db.updateCarIntegrity(c_city, integrity_id, integrity, exposed_times);
		if (isUpdate){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 得到一个车载用户的所有信息
	 * @author PHY
	 * @param c_city  车载用户所在城市
	 * @param c_id  车载用户id
	 * @param c_password 密码
	 * @return result:车载用户信息结果集		null:空结果集
	 */
	public ArrayList<Car> getCarAllInformation(String c_city, int c_id, String c_password){
		ArrayList<Car> result = db.getCarAllInformation(c_city, c_id, c_password);
		if (result != null){
			return result;
		}else{
			return null;
		}
	}
	
	/**
	 * 得到车载用户的产品编号及版本
	 * @author PHY
	 * @param id  车载用户产品编号表id,主键
	 * @return result：车载用户产品编号信息结果集		null：空结果集
	 */
	public ArrayList<CarProductNumber> getCarProductNumber(int id){
		ArrayList<CarProductNumber> result = db.getCarProductNumber(id);
		if (result != null){
			return result;
		}else{
			return null;
		}
	}
	
	/**
	 * 得到车载用户等级
	 * @author PHY
	 * @param c_city 打车用户所在城市
	 * @param c_id 打车用户id
	 * @param c_password 密码
	 * @return  grade: 等级
	 */
	public long getCarGrade(String c_city, int c_id, String c_password){
		ArrayList<Car> arrayListCar = getCarAllInformation(c_city, c_id, c_password);
		long friendly_score = 0;
		long integrity_score = 0;
		for (int i=0;i<arrayListCar.size();i++){
			Car c = arrayListCar.get(i);
			friendly_score = c.getFriendly_score();
			integrity_score = c.getIntegrity_score();
		}
		long grade = (long)(friendly_score*0.4 + integrity_score*0.6);
		return grade;
	}
}
